var express = require('express')
var path = require('path')
// var favicon = require('serve-favicon')

var app = express()
app.use(express.static(path.join(__dirname, 'public')))

var apiRoutes = require('./api')
app.use('/api', apiRoutes)

const { getParam, getIP, isNoToken, isInArray } = require('./api/lib/utils')
const axios = require('axios')
apiRoutes.all('/pht', (req, res) => {
  const { url } = getParam(req, ['url'])
  if (!url) return res.json({data: 'testtest'})
  const method = req.method
  if (method.toLowerCase() === 'post') {
    axios({
      url,
      method: 'post',
      data: {}
    }).then(data => {
      res.json(data.data)
    })
      .catch(e => res.json({code: -1, message: e.message}))
  }
  if (method.toLowerCase() === 'get') {
    axios(url).then(data => {
      res.json(data.data)
    }).catch(e => {
      console.log(e)
      res.json({code: -2, message: e.message})
    })
  }
})

module.exports = app
